package top.superflow.flow.runtime.context

import top.superflow.core.pool.resource.{ResourcePool, ResourcePoolManager}
import top.superflow.core.resource.PooledResource
import top.superflow.exception.RequestUnsupportedException

import scala.collection.Map

trait ResourceContext extends ContextHierachy{

  private val resources  = Map[String, ResourcePool[_]]()

  override def getParent: ResourceContext = {
    if(super.getParent == null){
      return null;
    }

    super.getParent.asInstanceOf[ResourceContext]
  }

  override def setParent ( c : ContextHierachy) : Unit = {
    if(c == null){
      return;
    }

    if(!c.isInstanceOf[ResourceContext]){
      throw new RequestUnsupportedException("Please pass in child type ResourceContext instead of ContextHierachy as the parent field.")
    }

    super.setParent(c)
  }

  def getResource(key : String) : PooledResource[_] = {
    val optionsResourcePool = resources.get(key)

    if(optionsResourcePool.isEmpty){
      return this.getParent.getResource(key)
    }

    val resourcePool = optionsResourcePool.get

    resourcePool.getResourceAction.get
  }

  def addResourcePool(r : ResourcePool[_]) = {

  }

  def getResourcePoolManager : ResourcePoolManager
}
